<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Maillist_SenderController extends Zend_Controller_Action{
	public function init(){
        parent::init();
		$this->getHelper('layout')->disableLayout(true);
                //setDisable(true);
    }
    
    public function templateAction(){
        
    }
    
    public function sendAction(){
        $postsMapper = new Maillist_Model_PostsMapper();
        $this->view->post = $postsMapper->fetchLastPost();
        
        if (null==$this->view->post) return;
        
        $mapper = new Maillist_Model_SubscribersMapper();
        $list = $mapper->fetchSubscribers4Sending($this->view->post->getId());
        
        if ((null==$list) || (count($list)==0)) return;
        
        $mail = new Zend_Mail('utf-8');
        $mail->setSubject($this->view->post->getSubject());
        $mail->setFrom("aricksoft-test@mail.ru", 'Сайт TE');
        foreach ($list as $subscr){
            $this->view->subscriber = $subscr;
            $body = $this->view->render('sender/template.phtml');
            $mail->clearRecipients();
            $mail->addTo($subscr->getEmail(),$subscr->getName1());
            $mail->setBodyHtml($body,'UTF-8',Zend_Mime::ENCODING_BASE64);
            $mail->send();
            $subscr->setPostId($this->view->post->getId());
            $mapper->save(array('Id'=>$subscr->getId(), 'PostId'=>$subscr->getPostId()));
        }
        $this->_helper->viewRenderer->setNoRender(true);
    }
}

?>